package com.hundsun.cprs.yyt.service.quota.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.quota.dao.QuotaDao;
import com.hundsun.cprs.yyt.service.quota.domain.Quota;
import com.hundsun.cprs.yyt.service.quota.domain.query.QuotaQuery;
import com.hundsun.cprs.yyt.service.quota.enums.EnumQuotaType;
import com.hundsun.cprs.yyt.service.quota.service.QuotaService;
import org.springframework.util.CollectionUtils;

/**
 * @author herong22384
 * @date 2019/8/5 16:42
 */
@Service
public class QuotaServiceImpl extends BaseService implements QuotaService {

    @Autowired
    private QuotaDao quotaDao;

    @Override
    public void createQuote(Quota quota) {
        validatParam(quota);//参数校验
        quota.setType(EnumQuotaType.SPECIAL.getCode());//后台新增的都是专用配额，通用配额类型只需要初始化一条即可
        quota.setIsDeleted("N");
        if (quota.getTradeAmountLimit() == null) {
        	quota.setTradeAmountLimit(BigDecimal.ZERO.longValue());
		}
        quotaDao.insert(quota);
    }

    @Override
    public void modifyQuote(Quota quota) {
        validatParam(quota);//参数校验
        quotaDao.updateByPrimaryKeySelective(quota);
    }

    @Override
    public void deleteQuote(Long quoteId) {
        int result = quotaDao.deleteByPrimaryKey(quoteId);
        if (result == 0){
            throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1006.getErrorNo(),EnumBusinessCode.BUSINESS_1006.getErrorInfo());
        }
    }

    @Override
    public void queryByPage(QuotaQuery query) {
        List<Quota> quotas = quotaDao.queryByPage(query);
        query.compatible(quotas);
    }

    @Override
    public Quota selectQuoteById(Long quoteId) {
        return quotaDao.selectByPrimaryKey(quoteId);
    }
    
	@Override
	public List<Quota> queryByList(QuotaQuery query) {
		query.onlyReturnList();
		return quotaDao.queryByPage(query);
	}

	@Override
	public List<Quota> queryAll() {
		QuotaQuery query = new QuotaQuery();
		return queryByList(query);
	}

    @Override
    public Quota getDefaultQuota() {
        QuotaQuery query = new QuotaQuery();
        query.setType(EnumQuotaType.COMMON.getCode());
        List<Quota> quotas = quotaDao.queryByPage(query);
        if (!CollectionUtils.isEmpty(quotas)){
            return quotas.get(0);
        }else {
            return null;
        }
    }

    protected void validatParam(Quota quota){
        Validate.notEmpty(quota.getName(),"配额类型名称不能为空");
        Validate.notEmpty(quota.getIsNeedFreeze(),"转换是否需要冻结不能为空");
        Validate.notNull(quota.getExchangeAmount(),"每次兑换数量不能为空");
        if (quota.getIsNeedFreeze().equals("0")){
            Validate.notNull(quota.getUnfreezeCycle(),"兑换周期不能为空");
            Validate.notNull(quota.getUnfreezeNumInTimes(),"每次解冻数量不能为空");
        }else {
            quota.setUnfreezeCycle(0L);
            quota.setUnfreezeNumInTimes(0L);
        }
        if (quota.getExchangeRate() == null){
            quota.setExchangeRate(1d);
        }
        if (quota.getUnfreezeNumInTimes() > quota.getExchangeAmount()){
            throw new ServiceCodeException(EnumBusinessCode.BUSINESS_3238.getErrorNo(),EnumBusinessCode.BUSINESS_3238.getErrorInfo());
        }
    }

}
